################
#PSRM: Explaining Support for Redistribution: Social Insurance Systems and Fairness
#
#Observational Data
#Part I: Income
#
#Verena Fetscher
#July 2022
####################

rm(list=ls())



##########################
#Load Data
##########################


#European Social Survey

#ESS1-7 Cumulative Data
#http://www.europeansocialsurvey.org/downloadwizard/
#(accessed 22-03-2017)
#Converted to stata12 file and saved as R file
load("ESSR1-R7.Rda")
##########################


#OECD 

#Calculation purchasing power parity 
#website https://data.oecd.org/conversion/purchasing-power-parities-ppp.htm
#(accessed 22-03-2017)
PPP<-read.csv("DP_LIVE_22032017110129581.csv", header = TRUE, sep = ",", quote = "\"",
              dec = ".")

names(PPP) <- c("LOCATION","INDICATOR","SUBJECT","MEASURE","FREQUENCY","TIME","Value","Flag.Codes")
##########################


##########################
#Reduces data frame
##########################
ESS<-data

ESS%>%
  select(cntry,essround,
         hinctnt,hinctnta,dweight,pspwght,pweight,
         iscoco,isco08,marital,maritala,maritalb,chldhhe,
         gincdif,pplstrd,gndr,uempla,eduyrs,agea,
         hincfel,health,lrscale,stflife,rtrd,hhmmb) -> data




##########################
#Recode income data
##########################

#ESS asks respondents to place their total net household income into a number of income bands 
#(12 bands in 2002-06, 10 bands in 2008-2014) 
#Tables givw yearly, monthly, or weekly figures

#Four steps
#1) Follwoing American politics literature, transform income bands into midpoints (e.g. Hout 2004)
#2) Impute top-coded income category by assuming upper tail of income distribution follows 
#   Pareto distribution (e.g. Kopczuk/Saez/Song 2010)
#3) Purchasing power varies across countries, convert country's currency into 2005 PPP-adjusted constant US dollars
#4) Distance between respondent income and mean income of country year 

data$income<-NA
data$income.PPP<-NA
data$income_dist<-NA

##########################
#Austria 2002
##########################

#1) Midpoints
table(data$hinctnt[data$cntry=="AT"])


#Austria R1 2002
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="N"]<-120000 

table(data$hinctnt[data$cntry=="AT"&data$essround==1])
table(data$cntry)
table(data$income[data$cntry=="AT"&data$essround==1])

##########################


#2) Interpolation top income
#Source http://piketty.pse.ens.fr/files/oldfichiers051211/enseig/ecoineg/CourseNotes2009-2010(D).htm
#Methodology M2: Standard log-linear interpolation methodology

#Waves years 2002-06 (12 income bands)  
interpolation<-function(income,low_limit){
  counts<-income
  
  #calculate percentages in each category
  nB<-(counts[[12]]/sum(counts))      #observations in highest category
  nB_1<-(counts[[11]]/sum(counts))    #observations in second highest category
  
  #Absolut income
  lB<-as.numeric(names(counts)[12])   #Lower limit highest bracket
  lB_1<-low_limit                     #lower limit second highest bracket
  
  #Equation for interpolation
  country_alpha<-(log((nB_1+nB)/nB))/(log(lB/lB_1))
  country_beta<-country_alpha/(country_alpha-1)
  
  top_country<-(lB*country_alpha)/(country_alpha-1)
  
  return(top_country)
}


#Waves years 2008-2014 (10 income bands)
interpolation_change<-function(income,low_limit){
  counts<-income
  
  #calculate percentages in each category
  nB<-(counts[[10]]/sum(counts))      #observations in highest category
  nB_1<-(counts[[9]]/sum(counts))    #observations in second highest category
  
  #Absolut income
  lB<-as.numeric(names(counts)[10])   #Lower limit highest bracket
  lB_1<-low_limit                     #lower limit second highest bracket
  
  #Equation for interpolation
  country_alpha<-(log((nB_1+nB)/nB))/(log(lB/lB_1))
  country_beta<-country_alpha/(country_alpha-1)
  
  top_country<-(lB*country_alpha)/(country_alpha-1)
  
  return(top_country)
}


select<-data$cntry=="AT"&data$essround==1
top_AT<-interpolation(table(data$income[select]),90000)


##########################
#Application
##########################

data$income[data$cntry=="AT"&data$essround==1&data$hinctnt=="N"]<-top_AT
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="AUT"&PPP$TIME==2005]

table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Austria 2004
##########################

#Austria R2 2004
country<-"AT"
round<-2

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$hinctnt[data$cntry==country&data$essround==round])
table(data$income[data$cntry==country&data$essround==round])
###########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top_AT<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top_AT    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="AUT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Austria 2006
##########################

#Austria R3 2006
country<-"AT"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top_AT<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top_AT    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="AUT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Austria 2008
##########################

#Austria R4 2008
table(data$cntry[data$essround==4])

#Austria R5 2010
table(data$cntry[data$essround==5])

#Austria R6 2012
table(data$cntry[data$essround==6])

#Austria R7 2014
table(data$cntry[data$essround==7])



country<-"AT"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1)Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((12600-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((12600+(18100-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((18100+(22800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((22800+(27100-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((27100+(32800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((32800+(38900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((38900+(45600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((45600+(54900-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((54900+(70800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-70800

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top_AT<-interpolation_change(table(data$income[select]),54900)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top_AT    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="AUT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Belgium 2002
##########################
table(data$cntry)


#Belgium R1 2002
country<-"BE"
round<-1

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="BEL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])


##########################
#Belgium 2004
##########################
table(data$cntry)


#Belgium R2 2004
country<-"BE"
round<-2

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################

#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################

#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="BEL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################

#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################


##########################
#Belgium 2006
##########################

#Belgium R3 2006
country<-"BE"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="BEL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Belgium 2008
##########################


#Belgium R4 2008
country<-"BE"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((5000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((5000+(10000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((10000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((12000+(14000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((14000+(16000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((16000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((18000+(21000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((21000+(26000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((26000+(35000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-35000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),26000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="BEL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Belgium 2010
##########################


#Belgium R5 2010
country<-"BE"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11040-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11040+(14160 -1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((14160+(17640 -1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((17640+(21360-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((21360+(25560-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((25560+(30600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((30600+(37440-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((37440+(44880-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((44880+(56760-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-56760

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),44880)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="BEL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Belgium 2012
##########################


#Belgium R6 2012
country<-"BE"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((12120-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((12120+(15330-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15330+(18880-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((18880+(22720-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((22720+(27300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((27300+(33430-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((33430+(40250-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((40250+(48000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((48000+(60230-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-60230

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),48000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="BEL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Belgium 2014
##########################

#Belgium R7 2014
country<-"BE"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])


#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((12560-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((12560+(15420-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15420+(19160-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((19160+(23200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((23200+(28000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((28000+(33900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((33900+(40880-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((40880+(49800-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((49800+(62360-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-62360

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),49800)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="BEL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################


##########################
#Switzerland 2002
##########################
table(data$cntry)

#Switzerland Round 1
country<-"CH"
round<-1

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((2760-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((2760+(5400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((5400+(9000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((9000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((18000+(27600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((27600+(36000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((36000+(45600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((45600+(54000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((54000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((90000+(135600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((135600+(180000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-180000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),135600)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

table(PPP$LOCATION)
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="CHE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Switzerland 2004
##########################

table(data$cntry)

#Switzerland R2 2004
country<-"CH"
round<-2

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((2700-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((2700+(5400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((5400+(9000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((9000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((18000+(27000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((27000+(36000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((36000+(45000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((45000+(54000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((54000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((90000+(135000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((135000+(180000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-180000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),135000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="CHE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Switzerland 2006
##########################

#Switzerland R3 2006
country<-"CH"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((2700-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((2700+(5400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((5400+(9000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((9000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((18000+(27000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((27000+(36000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((36000+(45000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((45000+(54000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((54000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((90000+(135000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((135000+(180000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-180000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),135000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="CHE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################


##########################
#Switzerland 2008
##########################

#Switzerland R4 2008
country<-"CH"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((31500-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((31500+(45000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((45000+(55500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((55500+(65000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((65000+(75500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((75500+(87500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((87500+(102000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((102000+(122000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((122000+(156500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-156500

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),122000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="CHE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Switzerland 2010
##########################

#SwitzerlandR5 2010
country<-"CH"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((32500-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((32500+(45500 -1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((45500+(56500 -1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((56500+(68000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((68000+(79500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((79500+(91500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((91500+(106000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((106000+(127500-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((127500+(165500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-165500

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),127500)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="CHE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Switzerland 2012
##########################

#Switzerland R6 2012
country<-"CH"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((34500-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((34500+(49500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((49500+(61500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((61500+(75000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((75000+(88000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((88000+(105000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((105000+(122000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((122000+(145000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((145000+(184500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-184500

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),145000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="CHE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################


##########################
#Switzerland 2014
##########################

#Switzerland R7 2014
country<-"CH"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((35000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((35000+(49000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((49000+(62000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((62000+(75000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((75000+(88000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((88000+(103000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((103000+(122000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((122000+(146000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((146000+(190000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-190000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),146000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="CHE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Germany 2002
##########################
table(data$cntry)

#Belgium R1 2002
country<-"DE"
round<-1

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DEU"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Germany 2004
##########################

table(data$cntry)

#Germany R2 2004
country<-"DE"
round<-2

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DEU"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Germany 2006
##########################

#Germany R3 2006
country<-"DE"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DEU"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Germany 2008
##########################

#Germany R4 2008
country<-"DE"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((13200)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((13201+(17500))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((17501+(22100))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((22101+(27000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((27001+(32500))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((32501+(38300))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((38301+(45200))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((45201+(54600))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((54601+(70400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-70401

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),54601)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DEU"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Germany 2010
##########################

#Germany R5 2010
country<-"DE"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11340)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11341+(15420))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15421+(18950))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((18951+(22630))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((22631+(26500))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((26501+(30700))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((30701+(35710))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((35711+(42380))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((42381+(53770))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-53771

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),42381)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DEU"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################


##########################
#Germany 2012
##########################

#Germany R6 2012
country<-"DE"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11770)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11771+(16140))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((16141+(19920))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((19921+(23880))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((23881+(28070))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((28071+(32780))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((32781+(38340))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((38341+(45830))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((45831+(58040))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-58041

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),45831)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DEU"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Germany 2014
##########################

#Belgium R7 2014
country<-"DE"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((12000)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((12001+(16560))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((16561+(20400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((20401+(24480))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((24481+(28800))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((28801+(33600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((33601+(39360))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((39361+(46920))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((46921+(59520))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-59521

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),46921)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DEU"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Denmark 2002
##########################

table(data$cntry)

#Denmark R1 2002
country<-"DK"
round<-1

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((13300-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((13300+(26600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((26600+(44400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((44400+(88800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((88800+(133200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((133200+(177600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((177600+(222000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((222000+(266400-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((266400+(444400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((444400+(666000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((666000+(888000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-888000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),666000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DNK"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################


##########################
#Denmark2004
##########################

table(data$cntry)

#Denmark R2 2004
country<-"DK"
round<-2

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((13300-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((13300+(26600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((26600+(44400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((44400+(88800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((88800+(133200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((133200+(177600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((177600+(222000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((222000+(266400-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((266400+(444400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((444400+(666000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((666000+(888000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-888000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),666000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DNK"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Denmark 2006
##########################

#Denmark R3 2006
country<-"DK"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((13300-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((13300+(26600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((26600+(44400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((44400+(88800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((88800+(133200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((133200+(177600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((177600+(222000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((222000+(266400-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((266400+(444400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((444400+(666000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((666000+(888000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-888000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),666000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DNK"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Denmark 2008
##########################

#Denmark R4 2008
country<-"DK"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((110000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((110000+(147999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((148000+(185999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((186000+(225999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((226000+(276999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((277000+(337999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((338000+(392999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((393000+(449999))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((450000+(533999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-534000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),450000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DNK"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Denmark 2010
##########################

#Denmark R5 2010
country<-"DK"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((121000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((121000+(159999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((160000+(198999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((199000+(241999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((242000+(297999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((298000+(363999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((364000+(423999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((424000+(485999))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((486000+(577999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-578000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),486000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DNK"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Denmark 2012
##########################

#Denmark R6 2012
country<-"DK"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((132000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((132000+(173999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((174000+(216999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((217000+(263999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((264000+(324999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((325000+(395999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((396000+(461999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((462000+(529999))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((530000+(629999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-630000
table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),530000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DNK"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Denmark 2014
##########################

#Denmark R7 2014
country<-"DK"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((132000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((132000+(173999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((174000+(217999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((218000+(263999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((264000+(325999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((326000+(396999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((397000+(462999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((463000+(530999))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((531000+(630999))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-631000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),531000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="DNK"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Spain 2002
##########################

table(data$cntry)

# R1 2002
country<-"ES"
round<-1

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000  

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ESP"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Spain 2004
##########################

table(data$cntry)

#Spain R2 2004
country<-"ES"
round<-2

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ESP"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Spain 2006
##########################

#Germany R3 2006
country<-"ES"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((3600+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((6000+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((12000+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((18000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((24000+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((30000+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((36000+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((60000+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((90000+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ESP"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Spain 2008
##########################

#Spain R4 2008
country<-"ES"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((8000)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((8001+(11000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((11001+(14000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((14001+(17000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((17001+(20000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((20001+(23000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((23001+(27000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((27001+(33000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((33001+(41000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-41001

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),33001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ESP"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Spain 2010
##########################

#Spain R5 2010
country<-"ES"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((8000)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((8001+(11000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((11001+(14000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((14001+(17000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((17001+(20000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((20001+(23000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((23001+(27000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((27001+(33000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((33001+(41000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-41001

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),33001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ESP"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Spain 2012
##########################

#Spain R6 2012
country<-"ES"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((9170)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((9171+(13980))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((13981+(15130))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((15131+(19720))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((19721+(20950))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((20951+(25700))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((25701+(28840))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((28841+(33070))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((33071+(44430))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-44431

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),33071)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ESP"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Spain 2014
##########################

#Spain R7 2014
country<-"ES"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((9000)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((9001+(11400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((11401+(14400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((14401+(16800))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((16801+(20400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((20401+(25200))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((25201+(27600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((27601+(33000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((33001+(42600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-42601
table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),33001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ESP"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Finland 2002
##########################

table(data$cntry)

#Finland R1 2002
country<-"FI"
round<-1

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-NA
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-NA
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((499)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((500+999)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((1000+(1499))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((1500+(1999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((2000+(2499))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((2500+(2999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((3000+(4999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((5000+(7499))/2)*12 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((7500+(10000))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-10000*12

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FIN"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
mean(data$income.PPP[select],na.rm=T)
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
mean(data$income_dist[select],na.rm=T)
##########################



##########################
#Finland 2004
##########################

table(data$cntry)

#Finland R2 2004
country<-"FI"
round<-2

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-(149/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((150+299)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((300+499)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((500+999)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((1000+(1499))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((1500+(1999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((2000+(2499))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((2500+(2999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((3000+(4999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((5000+(7499))/2)*12 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((7500+(10000))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-10000*12

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FIN"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
summary(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Finland 2006
##########################

#Finland R3 2006
country<-"FI"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-(149/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((150+299)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-((300+499)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-((500+999)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-((1000+(1499))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-((1500+(1999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-((2000+(2499))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-((2500+(2999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-((3000+(4999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-((5000+(7499))/2)*12 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-((7500+(10000))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-10000*12

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income
#Adapt function to only 11 income bands
interpolation_11<-function(income,low_limit){
  counts<-income
  
  #calculate percentages in each category
  nB<-(counts[[11]]/sum(counts))      #observations in highest category
  nB_1<-(counts[[10]]/sum(counts))    #observations in second highest category
  
  #Absolut income
  lB<-as.numeric(names(counts)[11])   #Lower limit highest bracket
  lB_1<-low_limit                     #lower limit second highest bracket
  
  #Equation for interpolation
  country_alpha<-(log((nB_1+nB)/nB))/(log(lB/lB_1))
  country_beta<-country_alpha/(country_alpha-1)
  
  top_country<-(lB*country_alpha)/(country_alpha-1)
  
  return(top_country)
}

select<-data$cntry==country&data$essround==round 
top<-interpolation_11(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FIN"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
summary(data$income.PPP[select])

##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Finland 2008
##########################

#Finland R4 2008
country<-"FI"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((900-1)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((900+(1199))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((1200+(1499))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((1500+(1799))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((1800+(2199))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((2200+(2599))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((2600+(3099))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((3100+(3699))/2) *12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((3700+(4599))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-4600*12

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),44400)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FIN"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Finland 2010
##########################

#Germany R5 2010
country<-"FI"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((959-1)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((959+(1231))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((1232+(1615))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((1616+(1970))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((1971+(2370))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((2371+(2886))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((2887+(3484))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((3484+(4159))/2) *12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((4160+(5142))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-5143*12

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),49920)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FIN"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Finland 2012
##########################


#Finland R6 2012
country<-"FI"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((1010-1)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((1010+(1292))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((1293+(1694))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((1695+(2070))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((2071+(2479))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((2480+(2986))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((2987+(3617))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((3618+(4311))/2) *12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((4312+(5361))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-5362*12

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),51744)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FIN"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Finland 2014
##########################

#Finland R7 2014
country<-"FI"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((1074)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((1075+(1366))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((1367+(1808))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((1809+(2215))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((2216+(2627))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((2628+(3157))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((3158+(3824))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((3825+(4555))/2)*12 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((4556+(5673))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-5674*12

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),54672)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FIN"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#France 2002
##########################

#France R1 2002
country<-"FR"
round<-1
table(data$hinctnt[data$cntry==country&data$essround==round])


#France R2 2004
country<-"FR"
round<-2
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600+1)+(6000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000+1)+(12000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000+1)+(18000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000+1)+(24000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000+1)+(30000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000+1)+(36000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000+1)+(60000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000+1)+(90000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000+1)+(120000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000+1 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000+1)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FRA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#France 2006
##########################
table(data$cntry)

#France R3 2006
country<-"FR"
round<-3
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600+1)+(6000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000+1)+(12000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000+1)+(18000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000+1)+(24000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000+1)+(30000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000+1)+(36000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000+1)+(60000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000+1)+(90000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000+1)+(120000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000+1 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FRA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#France 2008
##########################

#France R4 2008
country<-"FR"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11400)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11401+(14400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((14401+(18000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((18001+(21000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((21001+(24000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((24001+(28800))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((28801+(33600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((33601+(39600))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((39601+(49200))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-49201

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),39601)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FRA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#France 2010
##########################

#France R5 2010
country<-"FR"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((13200)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((13201+(16800))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((16801+(21000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((21001+(25200))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((25201+(30000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((30001+(34800))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((34801+(39600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((39601+(48000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((48001+(61200))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-61201
table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),48001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FRA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#France 2012
##########################

#France R6 2012
country<-"FR"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((13200)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((13201+(18000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((18001+(21600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((21601+(25200))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((25201+(30000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((30001+(36000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((36001+(42000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((42001+(49200))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((49201+(63600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-63601
table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),49201)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FRA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#France 2014
##########################

#France R7 2014
country<-"FR"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((13200)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((13201+(17040))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((17041+(20580))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((20581+(24600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((24601+(29400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((29401+(34560))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((34561+(40800))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((40801+(49200))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((49201+(63600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-63601
table(data$income[data$cntry==country&data$essround==round])


table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),49201)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="FRA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#GB 2002
##########################

#GB R1 2002
country<-"GB"
round<-1

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1190-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1190+(2380-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((2380)+(3970-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((3970)+(7950-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((7950)+(11920-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((11920)+(15890-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((15890)+(19870-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((19870)+(23840-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((23840)+(39740-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((39740)+(59600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((59600)+(79470-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-79470 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),59600)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="GBR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#GB 2004
##########################

#GB R2 2004
country<-"GB"
round<-2

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1190-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1190+(2380-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((2380)+(3970-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((3970)+(7950-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((7950)+(11920-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((11920)+(15890-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((15890)+(19870-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((19870)+(23840-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((23840)+(39740-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((39740)+(59600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((59600)+(79470-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-79470 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),59600)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="GBR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#GB 2006
##########################

#GB R3 2006
country<-"GB"
round<-3

table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1190-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1190+(2380-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((2380)+(3970-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((3970)+(7950-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((7950)+(11920-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((11920)+(15890-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((15890)+(19870-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((19870)+(23840-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((23840)+(39740-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((39740)+(59600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((59600)+(79470-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-79470 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),59600)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="GBR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#GB 2008
##########################

#GB R4 2008
country<-"GB"
round<-4

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((8550-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((8550+(11470-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((11470+(14440-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((14440+(17360-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((17360+(21120-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((21120+(25650-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((25650+(30870-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((30870+(38060-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((38060+(50110-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-50110

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),38060)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="GBR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#GB 2010
##########################

#GB R5 2010
country<-"GB"
round<-5

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((9390-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((9390+(12510-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((12510+(15590-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((15590+(18880-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((18880+(22940-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((22940+(27530-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((27530+(33060-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((33060+(41090-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((41090+(53920-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-53920

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),41090)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="GBR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#GB 2012
##########################

#GB R6 2012
country<-"GB"
round<-6

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((9850-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((9850+(13190-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((13190+(16320-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((16320+(19650-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((19660+(23520-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((23520+(28000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((28000+(33790-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((33790+(41350-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((41350+(54910-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-54910

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),41350)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="GBR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#GB 2014
##########################

#GB R7 2014
country<-"GB"
round<-7

table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((10858-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((10858+(14548-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((14548+(18132-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((18132+(21715-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((21715+(25994-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((25994+(30754-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((30754+(36691-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((36691+(44714-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((44714+(58620-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-58620

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),44714)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top    #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="GBR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Ireland 2002
##########################

#Ireland R1 2002
country<-"IE"
round<-1
table(data$hinctnt[data$cntry==country&data$essround==round])


#Ireland R2 2004
country<-"IE"
round<-2
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="IRL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################


##########################
#Ireland 2006
##########################

#Ireland R3 2006
country<-"IE"
round<-3
table(data$hinctnta[data$cntry==country&data$essround==round])


#Ireland R4 2008
country<-"IE"
round<-4
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((10000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((10000+(16000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((16000+(22000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((22000+(29000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((29000+(35000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((35000+(43000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((43000+(53000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((53000+(63000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((63000+(77000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-77000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),63000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="IRL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Ireland 2010
##########################

#Ireland R5 2010
country<-"IE"
round<-5
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((14173-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((14173+(20775-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((20775+(25577-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((25577+(32777-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((32777+(38174-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((38174+(45636-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((45636+(54851-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((54851+(64951-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((64951+(85526-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-85526

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),64951)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="IRL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Ireland 2012
##########################

#Ireland R6 2012
country<-"IE"
round<-6
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((13607-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((13607+(17881-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((17881+(24610-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((24610+(30042-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((30042+(35717-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((35717+(42399-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((42399+(51830-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((51830+(62818-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((62818+(83877-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-83877

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),61818)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="IRL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Ireland 2014
##########################

#Ireland R7 2014
country<-"IE"
round<-7
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((12816-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((12816+(17655-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((17655+(23551-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((23551+(28793-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((28793+(33506-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((33506+(40274-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((40274+(48170-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((48170+(58705-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((58705+(75072-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-75072

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),58705)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="IRL"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Italy 2002
##########################

#Italy R1 2002
country<-"IT"
round<-1
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################



#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ITA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



##########################
#Italy 2004
##########################

#Italy R2 2004
country<-"IT"
round<-2
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ITA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Italy 2006
##########################

#Italy R3 2006
country<-"IT"
round<-3
table(data$hinctnt[data$cntry==country&data$essround==round])


#Italy R4 2008
country<-"IT"
round<-4
table(data$hinctnt[data$cntry==country&data$essround==round])


#Italy R5 2010
country<-"IT"
round<-5
table(data$hinctnt[data$cntry==country&data$essround==round])


#Italy R6 2012
country<-"IT"
round<-6
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11710)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11711+(15632))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15633+(19200))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((19201+(23035))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((23036+(27000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((27001+(31952))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((31953+(37683))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((37684+(45340))/2) #mistake in showcard 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((45341+(58549))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-58550

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),45341)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="ITA"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Italy 2014
##########################

#Italy R7 2014
country<-"IT"
round<-7
table(data$hinctnta[data$cntry==country&data$essround==round])
##########################



##########################
#Netherlands 2002
##########################

#Netherlands R1 2002
country<-"NL"
round<-1
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NLD"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Netherlands 2004
##########################

#Netherlands R2 2004
country<-"NL"
round<-2
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NLD"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Netherlands 2006
##########################

#Netherlands R3 2006
country<-"NL"
round<-3
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NLD"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Netherlands 2008
##########################

#Netherlands R4 2008
country<-"NL"
round<-4
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11700-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11700+(15000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15000+(18200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((18200+(21500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((21500+(25300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((25300+(29500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((29500+(34200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((34200+(40200-1))/2) #mistake in showcard 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((40200+(50300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-50300

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),40200)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NLD"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Netherlands 2010
##########################

#Netherlands R5 2010
country<-"NL"
round<-5
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((13000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((13000+(17000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((17000+(20000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((20000+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((24000+(29000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((29000+(34000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((34000+(39000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((39000+(46000-1))/2) #mistake in showcard 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((46000+(58000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-58000

table(data$income[data$cntry==country&data$essround==round])
##########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),46000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
##########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NLD"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
##########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
##########################



##########################
#Netherlands 2012
#########################

#Netherlands R6 2012
country<-"NL"
round<-6
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11300-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11300+(15600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15600+(19300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((19300+(23700-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((23700+(27300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((27300+(32500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((32500+(38300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((38300+(44700-1))/2) #mistake in showcard 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((44700+(54400-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-54400

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),44700)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NLD"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Netherlands 2014
#########################

#Netherlands R7 2014
country<-"NL"
round<-7
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((13000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((13000+(17000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((17000+(20500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((20500+(24200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((24200+(28500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((28500+(33500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((33500+(39200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((39200+(46400-1))/2) #mistake in showcard 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((46400+(58200-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-58200

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),46400)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NLD"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Norway 2002
#########################

#Norway R1 2002
country<-"NO"
round<-1
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((12900-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((12900+(25800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((25800)+(43000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((43000)+(85900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((85900)+(128900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((128900)+(171800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((171800)+(214800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((214800)+(257700-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((257700)+(429500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((429500)+(644300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((644300)+(859000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-859000 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),644300)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NOR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Norway 2004
#########################

#Norway R2 2004
country<-"NO"
round<-2
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((12900-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((12900+(25800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((25800)+(43000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((43000)+(85900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((85900)+(128900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((128900)+(171800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((171800)+(214800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((214800)+(257700-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((257700)+(429500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((429500)+(644300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((644300)+(859000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-859000 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),644300)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NOR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Norway 2006
#########################

#Norway R3 2006
country<-"NO"
round<-3
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((12900-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((12900+(25800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((25800)+(43000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((43000)+(85900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((85900)+(128900-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((128900)+(171800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((171800)+(214800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((214800)+(257700-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((257700)+(429500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((429500)+(644300-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((644300)+(859000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-859000 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),644300)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NOR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Norway 2008
#########################

#Norway R4 2008
country<-"NO"
round<-4
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((125000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((125000+(170000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((170000+(220000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((220000+(270000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((270000+(325000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((325000+(400000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((400000+(475000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((475000+(550000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((550000+(670000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-670000

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),550000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NOR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Norway 2010
#########################

#Norway R5 2010
country<-"NO"
round<-5
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((205000)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((205001+(295000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((295001+(370000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((370001+(450000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((450001+(530000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((530001+(600000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((600001+(675000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((675001+(775000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((775001+(935000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-935001

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),775001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NOR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Norway 2012
#########################

#Norway R6 2012
country<-"NO"
round<-6
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((216000)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((216001+(311000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((311001+(390000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((390001+(470000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((470001+(550000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((550001+(626000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((626001+(706000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((706001+(807000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((807001+(973000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-973001

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),807001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NOR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Norway 2014
#########################

#Norway R7 2014
country<-"NO"
round<-7
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((231000)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((231001+(331000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((331001+(417000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((417001+(502000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((502001+(588000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((588001+(672000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((672001+(761000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((761001+(871000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((871001+(1053000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-1053001

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),871001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="NOR"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################


#########################
#Portugal 2002
#########################

#Portugal R1 2002
country<-"PT"
round<-1
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="PRT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Portugal 2004
#########################

#Portugal R2 2004
country<-"PT"
round<-2
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="PRT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Portugal 2006
#########################

#Portugal R3 2006
country<-"PT"
round<-3
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1800-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1800+(3600-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((3600)+(6000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((6000)+(12000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((12000)+(18000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((18000)+(24000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((24000)+(30000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((30000)+(36000-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((36000)+(60000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((60000)+(90000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((90000)+(120000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-120000 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),90000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="PRT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Portugal 2008
#########################

#Portugal R4 2008
country<-"PT"
round<-4
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((5000-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((5000+(7000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((7000+(9000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((9000+(11000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((11000+(13800-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((13800+(16000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((16000+(19500-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((19500+(24500-1))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((24500+(35000-1))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-35000

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),24500)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP
data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="PRT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Portugal 2010
#########################

#Portugal R5 2010
country<-"PT"
round<-5
table(data$hinctnta[data$cntry==country&data$essround==round])

#Portugal R6 2012
country<-"PT"
round<-6
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((5500-1)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((5500+(7500))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((7501+(10000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((10001+(12000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((12001+(14000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((14001+(17000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((17001+(20000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((20001+(25000))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((25001+(35000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-35001

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),25001)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="PRT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])



#########################
#Portugal 2014
#########################

#Portugal R7 2014
country<-"PT"
round<-7
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((5099)/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((5100+(7400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((7401+(9400))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((9401+(11600))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((11601+(14000))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((14001+(16750))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((16751+(20100))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((20101+(24900))/2) 
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((24901+(37800))/2)
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-37801

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),24901)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="PRT"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Sweden 2002
#########################

#Sweden R1 2002
country<-"SE"
round<-1
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1400-1)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1400+(2800-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((2800)+(4700-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((4700)+(9400-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((9400)+(14100-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((14100)+(19000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((19000)+(23000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((23000)+(28000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((28000)+(47000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((47000)+(70000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((70000)+(94000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-94000*12 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),840000)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="SWE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################


#########################
#Sweden 2004
#########################

#Sweden R2 2004
country<-"SE"
round<-2
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1400-1)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1400+(2800-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((2800)+(4700-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((4700)+(9400-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((9400)+(14100-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((14100)+(19000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((19000)+(23000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((23000)+(28000-1))/2)*12 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((28000)+(47000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((47000)+(70000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((70000)+(94000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-94000*12 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),70000*12)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="SWE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Sweden 2006
#########################

#Sweden R3 2006
country<-"SE"
round<-3
table(data$hinctnt[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnt=="J"]<-((1400-1)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="R"]<-((1400+(2800-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="C"]<-(((2800)+(4700-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="M"]<-(((4700)+(9400-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="F"]<-(((9400)+(14100-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="S"]<-(((14100)+(19000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="K"]<-(((19000)+(23000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="P"]<-(((23000)+(28000-1))/2)*12 
data$income[data$cntry==country&data$essround==round&data$hinctnt=="D"]<-(((28000)+(47000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="H"]<-(((47000)+(70000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="U"]<-(((70000)+(94000-1))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-94000*12 

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation(table(data$income[select]),70000*12)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnt=="N"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="SWE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Sweden 2008
#########################

#Sweden R4 2008
country<-"SE"
round<-4
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((8099)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((8100+(10199))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((10200+(12399))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((12400+(14999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((15000+(17699))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((17700+(20999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((21000+(26299))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((26300+(33099))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((33100+(41199))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-41200*12

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),397200)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="SWE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Sweden 2010
#########################

#Sweden R5 2010
country<-"SE"
round<-5
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((11499)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11500+(13999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((14000+(16999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((17000+(19999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((20000+(23999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((24000+(27999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((28000+(31999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((32000+(35999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((36000+(43999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-44000*12

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),36000*12)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="SWE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Sweden 2012
#########################

#Sweden R6 2012
country<-"SE"
round<-6
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((10999)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11000+(14999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15000+(18999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((19000+(21999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((22000+(24999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((25000+(28999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((29000+(32999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((33000+(39999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((40000+(48999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-49000*12

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),40000*12)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="SWE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean

mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################



#########################
#Sweden 2014
#########################

#Sweden R7 2014
country<-"SE"
round<-7
table(data$hinctnta[data$cntry==country&data$essround==round])

#1) Midpoints
data$income[data$cntry==country&data$essround==round&data$hinctnta=="J - 1st decile"]<-((10999)/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="R - 2nd decile"]<-((11000+(14999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="C - 3rd decile"]<-((15000+(18999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="M - 4th decile"]<-((19000+(21999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="F - 5th decile"]<-((22000+(24999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="S - 6th decile"]<-((25000+(28999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="K - 7th decile"]<-((29000+(32999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="P - 8th decile"]<-((33000+(39999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="D - 9th decile"]<-((40000+(48999))/2)*12
data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-49000*12

table(data$income[data$cntry==country&data$essround==round])
#########################


#2) Interpolation top income

select<-data$cntry==country&data$essround==round                      
top<-interpolation_change(table(data$income[select]),40000*12)         #change

data$income[data$cntry==country&data$essround==round&data$hinctnta=="H - 10th decile"]<-top   #change
table(data$income[select])
#########################


#3) Adjust to PPP

data$income.PPP[select]<-(data$income[select])/PPP$Value[PPP$LOCATION=="SWE"&PPP$TIME==2005]  #change location
table(data$income.PPP[select])
#########################


#4) Distance to mean
mean(data$income.PPP[select],na.rm=T)
data$income_dist[select]<-data$income.PPP[select]-mean(data$income.PPP[select],na.rm=T)
table(data$income_dist[select])
#########################




#########################
#Country selection
#########################

summary(data$income_dist)

data<-data[data$cntry=="AT"|data$cntry=="BE"|data$cntry=="CH"|data$cntry=="DE"|
             data$cntry=="DK"|data$cntry=="ES"|data$cntry=="FI"|data$cntry=="FR"|
             data$cntry=="GB"|data$cntry=="IE"|data$cntry=="IT"|data$cntry=="NL"|
             data$cntry=="NO"|data$cntry=="PT"|data$cntry=="SE",]

summary(data$income)

data %>%
  group_by(cntry,essround) %>%
  summarise(income=mean(income.PPP,na.rm=T))


#########################
#Save file
#########################


save(data,file="DataFile_01_Income.Rda")
#########################


